package com.ooqiu.gaming.ui.preview;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.ooqiu.gaming.R;

import java.util.ArrayList;

@Route(path = PreviewPicActivity.PATH)
public class PreviewPicActivity extends AppCompatActivity {
    public static final String PATH = "/gn/image_preview";


    ArrayList<String> imgPaths = new ArrayList<>();
    Context mContext;

    @Autowired
    int position = 0;
    @Autowired
    String img;

//    public static void start(ArrayList<String> imgPaths, int pos) {
//
//        ARouter.getInstance().build(PreviewPicActivity.PATH)
//                .withStringArrayList("imgPaths", imgPaths)
//                .withInt("position", pos)
//                .navigation();
//    }

    public static void start(String imgPath) {
        ARouter.getInstance().build(PreviewPicActivity.PATH)
                .withString("img", imgPath)
                .navigation();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
        setContentView(R.layout.activity_preview_pic);
        this.mContext = this;

        ARouter.getInstance().inject(this);
//        imgPaths.addAll(getIntent().getStringArrayListExtra("imgs"));
//
//        pos = getIntent().getIntExtra("position", 0);
//
//
//        String img = getIntent().getStringExtra("img");
//


        if (!TextUtils.isEmpty(img)) {
            imgPaths.add(img);
        }
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(new SamplePagerAdapter());
        viewPager.setCurrentItem(position);

    }

    private class SamplePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imgPaths.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            Glide.with(mContext).load(imgPaths.get(position)).into(photoView);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
